﻿using MediatR;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Domains.Manages.Addr;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;
using static SlaughterItem.Common.Dtos.BusinessPageDto;

namespace GetSlaughterItem.API.Application.Commands.Week01.Manageres
{
    public class BusinesQueryHandler : IRequestHandler<BusinesQueryCommand, Result>
    {

        private readonly IBaseRepositoryGet<Provibces> Provibces;
        private readonly IBaseRepositoryGet<Citys> Citys;
        private readonly IBaseRepositoryGet<Areas> Areas;
        private readonly IBaseRepositoryGet<Business> Business;
        private readonly IBaseRepositoryGet<Industrytags> Industrytags;

        public BusinesQueryHandler(IBaseRepositoryGet<Provibces> Provibces, IBaseRepositoryGet<Citys> Citys,
            IBaseRepositoryGet<Areas> Areas, IBaseRepositoryGet<Business> Business, IBaseRepositoryGet<Industrytags> Industrytags)
        {
            this.Provibces = Provibces;
            this.Citys = Citys;
            this.Areas = Areas;
            this.Business = Business;
            this.Industrytags = Industrytags;
        }
        /// <summary>
        /// 企业查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BusinesQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
           
            try
            {
                var res = (from a in Business.GetAll()
                           join b in Provibces.GetAll() on a.ProvibceId equals b.ProvibceId
                           join c in Citys.GetAll() on a.CityId equals c.CityId
                           join d in Areas.GetAll() on a.AreaId equals d.AreaId
                           join e in Industrytags.GetAll() on a.IndustrytagId equals e.IndustrytagId
                           where a.IsDelete == false
                           select new BusinessQuery()
                           {
                               BusinessId = a.BusinessId,
                               BusinessName = a.BusinessName,
                               BusinessAbbreviation = a.BusinessAbbreviation,
                               BusinessDuty = a.BusinessDuty,
                               BusinessPhone = a.BusinessPhone,
                               AreaName = d.AreaName,
                               CitysName = c.CitysName,
                               ProvibceName = b.ProvibceName,
                               DetailedAddress = a.DetailedAddress,
                               IndustrytagName = e.IndustrytagName

                           }).ToList();


                if (!string.IsNullOrEmpty(request.BusinessName))
                {
                    res = res.Where(x => x.BusinessName.Contains(request.BusinessName)).ToList();
                }

                var totalCount = res.Count;
                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                var list = res.OrderBy(x => x.BusinessId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                var businesdata = new
                {
                    list,
                    totalCount,
                    totalPage
                };
              
                result.Data = businesdata;
                result.Msg = "查询成功";

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
